package com.dftdla.mq.publisher;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.dftdla.dao.pojo.ChatMsg;
import com.dftdla.server.message.Message;
import com.dftdla.utils.RedisKey;
import com.dftdlaRedis.cache.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author 14501
 */
@Slf4j
@Component
public class RabbitMqPublisher {

    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private RedisCache redisCache;
    @Resource
    private RedisTemplate redisTemplate;

    public void sendMsg(List<Long> userIds, Object o){
        if(userIds == null || userIds.size() == 0 || o == null){
            return;
        }
        boolean res = (boolean) redisTemplate.execute(new SessionCallback<Boolean>() {
            @Override
            public Boolean execute(@NotNull RedisOperations operations) throws DataAccessException {
                try {
                    operations.multi();
                    for (Long userId : userIds) {
                        ArrayList<Object> list = new ArrayList<>();
                        list.add(o);
                        operations.opsForList().rightPushAll(RedisKey.userMsgAutoKey(userId), list);
                    }
                    operations.exec();
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
        });
        if(!res){
            log.error("消息提交到Redis失败");
        }else{
            log.info("开始消息分发");
            for(Long userId: userIds){
                JSONObject json = new JSONObject();
                json.put("userId", userId);
                rabbitTemplate.convertAndSend("msg.topic","single.1", JSON.toJSONString(json));
            }
            log.info("消息分发完成");
        }
    }

    @Deprecated
    public void sendGroupMsg(List<Long> userIds){
        JSONObject json = new JSONObject();
        json.put("userIds",userIds);
        rabbitTemplate.convertAndSend("msg.topic","group.1",JSON.toJSONString(json));
    }

    @Deprecated
    public void sendSingleMsg(Long userId, ChatMsg msg){
        JSONObject json = new JSONObject();
        json.put("userId", userId);
        rabbitTemplate.convertAndSend("msg.topic","single.1", JSON.toJSONString(json));

        json = new JSONObject();
        json.put("msg",msg);
        redisCache.setCacheObject(RedisKey.userMsgKey(userId), json);
    }
}
